package com.kuangstudy.controller.index;

import com.kuangstudy.service.blogcategory.IBlogCategoryService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @author zhangfushuai
 * @description 首页Controller
 * @since 2022/4/18 09:54
 **/
@Controller
@Log4j2
public class IndexController {

    @Autowired
    private IBlogCategoryService blogCategoryService;

    /**
     * @author zhangfushuai
     * @description 访问首页 - 追加式加载更多
     * @since 2022/4/18 09:54
     * 参数 cid
     * 参数 session
     * 参数 request
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping(value = {"/", "/index"})
    public ModelAndView index(String cid, HttpSession session, HttpServletRequest request) {
        log.info("2222222222222222------>index 执行进来了");
        ModelAndView modelAndView = new ModelAndView();
        // 4: 把查询分类放入到数据模型中
        modelAndView.addObject("blogCategories", blogCategoryService.findBlogCategies());//100ms
        modelAndView.addObject("cid", Optional.ofNullable(cid).orElse("0"));
        // 5: 跳转视图模板
        modelAndView.setViewName("index/index");
        return modelAndView;
    }

    /**
     * @author zhangfushuai
     * @description 访问首页 - 追加式加载更多
     * @since 2022/4/18 09:54
     * 参数 cid
     * 参数 session
     * 参数 request
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping(value = {"/cover", "/indexCover"})
    public ModelAndView indexCover(String cid, HttpSession session, HttpServletRequest request) {
        log.info("2222222222222222------>index 执行进来了");
        ModelAndView modelAndView = new ModelAndView();
        // 4: 把查询分类放入到数据模型中
        modelAndView.addObject("blogCategories", blogCategoryService.findBlogCategies());//100ms
        modelAndView.addObject("cid", Optional.ofNullable(cid).orElse("0"));
        // 5: 跳转视图模板
        modelAndView.setViewName("index/pageindex-覆盖式的分页");
        return modelAndView;
    }
}
